<?php

namespace App\Service;
use http\Client\Curl\User;
use Hyperf\DbConnection\Db;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Cache\Annotation\CachePut;
use Hyperf\Utils\ApplicationContext;
use App\Service\UserService;

class TaskService extends CommicService
{

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }
    public function sign_cofig($applet_id){
        # 签到配置
        $redis_key = "data:sign:cofig";
        $sign_cofig_list = $this->redisService->hGet($redis_key,$applet_id);
        $sign_cofig_list = json_decode($sign_cofig_list,true);
        if (empty($sign_cofig_list)){
            # 签到配置
            $sign_cofig_list = Db::table("data_sign_config")->where([
                "applet_id"=>$applet_id
            ])->select("id","ml_coin")->orderBy("sort","desc")->get();
            $sign_cofig_list = $this->object_to_array($sign_cofig_list);
            if (empty($sign_cofig_list)){
                $sign_cofig_list = Db::table("data_sign_config")->where([
                    "applet_id"=>0
                ])->select("id","ml_coin")->orderBy("sort","desc")->get();
                $sign_cofig_list = $this->object_to_array($sign_cofig_list);
            }
            $this->redisService->hSet($redis_key,$applet_id,json_encode($sign_cofig_list));
        }
        return $sign_cofig_list;
    }
    public function sign_list($applet_id,$user_id){
        # 签到列表
        # 当前用户是否存在
        $redis_key = "data:sign:user:list:".$applet_id.":".$user_id;
        $user_sign_list = $this->redisService->get($redis_key);
        $user_sign_list = json_decode($user_sign_list,true);
        if ($user_sign_list){
            # 判断昨天签到吗？
            $min_day = $user_sign_list[0]["dates"];
            $max_key = count($user_sign_list)-1;
            $max_day = $user_sign_list[$max_key]["dates"];
            $today = date("Ymd");
            $yesterday = date("Ymd",strtotime("-1 day"));
            $yesterday_sigin_status = 1;
            echo "最小天数".$min_day.PHP_EOL;
            echo "最大天数".$max_day.PHP_EOL;
            if ($min_day>$today){
                $yesterday_sigin_status = $this->get_day_sing_status($applet_id,$user_id,$yesterday);
                echo "昨天签到状态".$yesterday_sigin_status;
                if ($yesterday_sigin_status==0){
                    echo "最小天数".PHP_EOL;
                    # 数据重置
                    $user_sign_list = [];
                    $this->del_user_sing_status($applet_id,$user_id);
                }
            }

            if ($today>$min_day){
                $yesterday_sigin_status = $this->get_day_sing_status($applet_id,$user_id,$yesterday);
                echo "昨天签到状态".$yesterday_sigin_status;
                if ($yesterday_sigin_status==0){
                    echo "最小天数".PHP_EOL;
                    # 数据重置
                    $user_sign_list = [];
                    $this->del_user_sing_status($applet_id,$user_id);
                }
            }

            if ($yesterday_sigin_status==1){
                # 数据重置
                if ($today>$max_day){
                    echo "最大天数".PHP_EOL;
                    $user_sign_list = [];
                    $this->del_user_sing_status($applet_id,$user_id);
                }
            }
        }
        if (empty($user_sign_list)){
            $user_sign_list = [];
            $sign_config_list = $this->sign_cofig($applet_id);
            foreach ($sign_config_list as $key=>$value){
                $value["status"] = 0;
                if ($key>0){
                    $s_day = $key;
                    print_r($s_day).PHP_EOL;
                    $value["dates"] = date("Ymd",strtotime("+".$s_day."day"));
                }else{
                    $value["dates"] = date("Ymd");
                }
                $user_sign_list[$key] = $value;
            }
            $this->redisService->set($redis_key,json_encode($user_sign_list));
            $numbers = count($sign_config_list) -1;
            $exp_time = $this->get_exp_time($numbers);
            $this->redisService->expire($redis_key,$exp_time);
        }
        return $user_sign_list;
    }

    public function update_sign_list($applet_id,$user_id){
        # 签到列表
        # 当前用户是否存在
        $redis_key = "data:sign:user:list:".$applet_id.":".$user_id;
        $user_sign_list = $this->sign_list($applet_id,$user_id);
        $datas = date("Ymd");
        $ml_coin = 0;
        foreach ($user_sign_list as $key =>$value){
            if ($value["dates"] ==$datas){
                $user_sign_list[$key]["status"] = 1;
                $ml_coin = $value["ml_coin"];
            }
        }
        $this->redisService->set($redis_key,json_encode($user_sign_list));
        $numbers = count($user_sign_list) -1;
        $exp_time = $this->get_exp_time($numbers);
        $this->redisService->expire($redis_key,$exp_time);
        # 优化后期是否任务队列操作
        $adddata = [
            "applet_id"=>$applet_id,
            "user_id"=>$user_id,
            "ml_coin"=>$ml_coin,
            "recharge_type"=>3,
        ];
        echo "充值数据:".PHP_EOL;
        $this->add_user_log($adddata);
        $this->userService->Inc_coin($applet_id,$user_id,["give_coin"=>$ml_coin]);
    }
    public function sign($applet_id,$user_id){
        # 签到
        $redis_key = "data:sign:user:log:"."$applet_id".":".$user_id;
        $datas = date("Ymd");
        $status = $this->redisService->hGet($redis_key,$datas);
        if (empty($status)){
            $ext_key = $this->redisService->exists($redis_key);
            $this->redisService->hSet($redis_key,$datas,"1");
            # 更新列表信息
            $this->update_sign_list($applet_id,$user_id);
            if (empty($ext_key)){
                $sign_list_key = "data:sign:user:list:".$applet_id.":".$user_id;
                $time = $this->redisService->ttl($sign_list_key);
                echo "列表有效时间:".$time;
                $this->redisService->expire($redis_key,$time);
            }
        }
//        $rec_data["sign_status"] = !empty($status) ? (int)$status : 0;
        $rec_data["sign_status"] = 1;
        return $rec_data;
    }

    public function get_day_sing_status($applet_id,$user_id,$datas){
        # 查看当天是否签到
        $redis_key = "data:sign:user:log:"."$applet_id".":".$user_id;
        $status = $this->redisService->hGet($redis_key,$datas);
        return !empty($status)? 1 : 0;
    }

    public function del_user_sing_status($applet_id,$user_id){
        echo "删除数据".PHP_EOL;
        # 删除以前签到数据
        $redis_key = "data:sign:user:log:"."$applet_id".":".$user_id;
        $this->redisService->del($redis_key);

    }
    public function task_cofig($applet_id){
        # 任务配置
        $redis_key = "data:task:cofig";
        $task_cofig = $this->redisService->hGet($redis_key,$applet_id);
        $task_cofig = json_decode($task_cofig,true);
        if (empty($task_cofig)){
            $applet_info = $this->get_applet_info($applet_id);
            $task_cofig = Db::table("data_task_config")->where(["applet_id"=>$applet_id])->select("id","name","intro","number","ml_coin","stype")->orderBy("sort","desc")->get();
            $task_cofig = $this->object_to_array($task_cofig);
            if (empty($task_cofig)){
                $task_cofig = Db::table("data_task_config")->where(["applet_id"=>"0"])->select("id","name","intro","number","ml_coin","stype")->orderBy("sort","desc")->get();
                $task_cofig = $this->object_to_array($task_cofig);
            }
            foreach ($task_cofig as $key=>$value){
                $task_cofig[$key]["name"] = str_replace("看点",$applet_info["coin_name"],$value["name"]);
                $task_cofig[$key]["intro"] = str_replace("看点",$applet_info["coin_name"],$value["intro"]);
            }
            $this->redisService->hSet($redis_key,$applet_id,json_encode($task_cofig));
        }
        return $task_cofig;

    }

    public function task_list($applet_id,$user_id){
        #任务列表
        $user_id = (string)$user_id;
        $redis_key = "data:task:user:list:".$applet_id.":".date("Ymd");
        $user_task_list = $this->redisService->hGet($redis_key,$user_id);
        $user_task_list = json_decode($user_task_list,true);
        if (empty($user_task_list)){
            $user_task_list = $this->task_cofig($applet_id);
            foreach ($user_task_list as $key=>$value){
                $user_task_list[$key]["task_status"] = 0;
                $user_task_list[$key]["user_number"] = 0;
            }
            $ext_key = $this->redisService->exists($redis_key);
            $this->redisService->hSet($redis_key,$user_id,json_encode($user_task_list));
            if (empty($ext_key)){
                $exp_time = $this->get_tday_exp_time();
                $this->redisService->expire($redis_key,$exp_time);
            }
        }
        return $user_task_list;
    }

    public function do_task($applet_id,$user_id,$stype){
        # 做任务
        $user_task_list = $this->task_list($applet_id,$user_id);
        $user_id = (string)$user_id;
        $redis_key = "data:task:user:list:".$applet_id.":".date("Ymd");
        $user_flg = 0;
        foreach ($user_task_list as $key =>$value){
            $flg = 0;
            if ($value["stype"] == $stype && $value["number"] !=$value["user_number"] && $value["task_status"] == 0){
                $value["user_number"] = $value["user_number"]+1;
                $flg = 1;
            }
            if ($flg ==1){
                $user_flg = 1;
                if ($value["number"] == $value["user_number"] && $value["task_status"] == 0){
                    $value["task_status"] = 1;
                }
                $user_task_list[$key] = $value;

            }
        }
        if ($user_flg==1){
            $this->redisService->hSet($redis_key,$user_id,json_encode($user_task_list));
        }

        return ["task_status"=>1];
    }

     public function  complete($applet_id,$user_id,$task_id){
        #完成任务
         $user_task_list = $this->task_list($applet_id,$user_id);
         $user_id = (string)$user_id;
         $redis_key = "data:task:user:list:".$applet_id.":".date("Ymd");
         $task_info = [];
         $falg =  0 ;
         foreach ($user_task_list as $key=>$value){
             if ($value["id"] == $task_id){
                 $task_info = $value;
                 if ($value["task_status"] == 0){
                     $falg = 1;
                     $user_task_list[$key]["task_status"] = 1;
                     $user_task_list[$key]["user_number"] = $value["number"];
                 }

             }
         }
         if (empty($task_info)){
             return 20041;
         }
         if ($falg == 1){
             $this->redisService->hSet($redis_key,$user_id,json_encode($user_task_list));
             return 10000;
         }else{
             return 10000;
         }


     }
    public function collect($applet_id,$user_id,$task_id){
        # 领取任务

        $user_task_list = $this->task_list($applet_id,$user_id);
        $user_id = (string)$user_id;
        $redis_key = "data:task:user:list:".$applet_id.":".date("Ymd");
        $task_info = [];
        foreach ($user_task_list as $key=>$value){
            if ($value["id"] == $task_id){
                $task_info = $value;
                $user_task_list[$key]["task_status"] = 2;
            }
        }
        if (empty($task_info)){
            return 20041;
        }
        if ($task_info["task_status"]==1){
            $this->redisService->hSet($redis_key,$user_id,json_encode($user_task_list));
            $add_data = [
                "name"=>"任务奖励-".$task_info["name"],
                "applet_id"=>$applet_id,
                "user_id"=>$user_id,
                "ml_coin"=>$task_info["ml_coin"],
                "recharge_type"=>3,
            ];
            $this->add_user_log($add_data);
            $this->userService->Inc_coin($applet_id,$user_id,["give_coin"=>$task_info["ml_coin"]]);
            return 10000;
        }elseif ($task_info["task_status"]==2){
            return 20042;
        }
        return 20043;

    }

}